package cn.aib.educenter.service.impl;

import cn.aib.commonutils.JwtUtils;
import cn.aib.commonutils.MD5;
import cn.aib.educenter.entity.UcenterMember;
import cn.aib.educenter.entity.vo.RegisterVo;
import cn.aib.educenter.mapper.UcenterMemberMapper;
import cn.aib.educenter.service.UcenterMemberService;
import cn.aib.servicebase.exception.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-01-16
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public String login(UcenterMember member) {
        String phone = member.getMobile();
        String password = member.getPassword();

        //非空判断
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(password)) { //若手机号或密码为空
            throw new GuliException(20001, "手机号和密码不能为空");
        }

        //手机号判断
        //根据手机号查询用户，用户不存在则抛异常
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();

        wrapper.eq("mobile", phone);

        UcenterMember exitMember = baseMapper.selectOne(wrapper);

        if (exitMember == null) {  //若用户不存在
            throw new GuliException(20001, "用户不存在");
        }

        //密码判断
        if (!exitMember.getPassword().equals(MD5.encrypt(password))) { //若密码和传入的密码不一致
            throw new GuliException(20001, "密码错误");
        }

        //禁用判断
        if (exitMember.getIsDisabled()) {
            throw new GuliException(20001, "该用户账号不可用");
        }

        //根据用户ID和昵称 获取token
        String token = JwtUtils.getJwtToken(exitMember.getId(), exitMember.getNickname());

        //返回token
        return token;
    }

    @Override
    public void regist(RegisterVo registerVo) {
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        /**
         *  非空判断
         */
        if(StringUtils.isEmpty(nickname) || StringUtils.isEmpty(mobile)
                || StringUtils.isEmpty(password) || StringUtils.isEmpty(code)){
            throw new GuliException(20001,"字段不能为空");
        }

        /**
         *  验证码判断
         *      1.根据手机号，查询redis的验证码
         *          》验证码存在，放行
         *          》验证码不存在，抛出异常
         *      2.比较验证码是否一致
         */
        String redisCode = redisTemplate.opsForValue().get(mobile);

        if(StringUtils.isEmpty(redisCode) || !redisCode.equals(code)){
            throw new GuliException(20001,"验证码错误");
        }
        /**
         *  手机号判断
         *      根据手机号查用户是否大于0，大于0代表用户存在，则抛出异常
         */
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();

        wrapper.eq("mobile", mobile);

        Integer count = baseMapper.selectCount(wrapper);

        if(count > 0){
            throw new GuliException(20001,"用户已存在");
        }

        /**
         *  插入数据库
         *      》补全信息后插入
         */

        UcenterMember member = new UcenterMember();

        //补全信息
        member.setMobile(mobile);
        member.setPassword(MD5.encrypt(password));
        member.setNickname(nickname);
        member.setIsDisabled(false);
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/3HEmJwpSzguqqAyzmBwqT6aicIanswZibEOicQInQJI3ZY1qmu59icJC6N7SahKqWYv24GvX5KH2fibwt0mPWcTJ3fg/132");

        baseMapper.insert(member);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer getRegisterFrom(String date) {

        Integer count = baseMapper.getRegisterFrom(date);

        return count;
    }
}
